package com.rybbaby.tss.platform.tags.pojo;

import com.rybbaby.tss.core.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @description 操作权限
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年6月20日 下午8:13:46
 */
public class Permission implements Cloneable {
	
	/** 页面名称 **/
	private String pageName;
	/** 按钮编码 **/
	private String code;
	/** 备注 **/
	private String remark;
	/** 按钮标签元素 **/
	private String tagName;
	/** 按钮名 **/
	private String innerHtml;
	/** 按钮class 属性 **/
	private List<String> classes = new ArrayList<String>();
	/** 按钮Id属性 **/
	private String id;
	/** 自定义属性 **/
	private List<Map<String , Object>> attrs = new ArrayList<Map<String , Object>>();
	/** 自定行内样式 **/
	private List<Map<String , Object>> styles = new ArrayList<Map<String , Object>>();
	/** 事件绑定 **/
	private Map<String , String> events = new HashMap<String , String>();
	
	/**
	 * 
	 * @return 页面名称
	 */
	public String getPageName() {
		return pageName;
	}
	
	/**
	 * 设置页面名称
	 * 
	 * @param pageName
	 *            页面名称
	 */
	public void setPageName(String pageName) {
		this.pageName = pageName;
	}
	
	public String getCode() {
		return code;
	}
	
	public void setCode(String code) {
		this.code = code;
	}
	
	public String getRemark() {
		return remark;
	}
	
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	/**
	 * @return 标签元素名称
	 */
	public String getTagName() {
		return tagName;
	}
	
	/**
	 * @param tagName
	 *            标签元素
	 */
	public void setTagName(String tagName) {
		this.tagName = tagName;
	}
	
	/**
	 * @return 按钮内部html
	 */
	public String getInnerHtml() {
		return innerHtml;
	}
	
	/**
	 * @param btnName
	 *            按钮内部html
	 */
	public void setInnerHtml(String btnName) {
		this.innerHtml = btnName;
	}
	
	/**
	 * @return 按钮Id属性
	 */
	public String getId() {
		if(this.id == null) {
			return "";
		}
		return id;
	}
	
	/**
	 * @param id
	 *            按钮Id属性
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * @return 按钮class 属性
	 */
	public String getClasses() {
		if(this.classes == null) {
			return "";
		}
		return StringUtils.join(this.classes , " ");
	}
	
	/**
	 * 添加 按钮class 属性
	 * 
	 * @param clas
	 *            按钮class 属性
	 * @return
	 */
	public boolean addClasse(String clas) {
		if(this.classes == null) {
			this.classes = new ArrayList<String>();
		}
		if(!this.classes.contains(clas)) {
			return this.classes.add(clas);
		} else {
			return false;
		}
	}
	
	/**
	 * @return 自定行内样式
	 */
	public String getStyles() {
		String separator = ":";
		List<Map<String , Object>> list = this.styles;
		return getValue(separator , list , false);
	}
	
	/**
	 * @return 自定义属性
	 */
	public String getAttrs() {
		String separator = "=";
		List<Map<String , Object>> list = this.attrs;
		return getValue(separator , list , true);
	}
	
	/**
	 * @return 事件绑定
	 */
	public String getEvents() {
		if(this.events == null || this.events.isEmpty()) {
			return "";
		}
		StringBuffer eventsSb = new StringBuffer();
		Set<String> keySet = this.events.keySet();
		for(String key : keySet) {
			String triggerFunction = this.events.get(key);
			eventsSb.append(" " + key + "=\"" + triggerFunction + "\"");
		}
		return eventsSb.toString();
	}
	
	/**
	 * 添加自定义属性
	 * 
	 * @param attrKey
	 *            属性key
	 * 
	 * @param attrValue
	 *            属性值
	 */
	public void addAttr(String attrKey , String attrValue) {
		addValue(attrKey , attrValue , this.attrs);
	}
	
	/**
	 * 添加行内样式
	 * 
	 * @param styleKey
	 *            样式key
	 * @param styleValue
	 *            样式值
	 */
	public void addStyle(String styleKey , String styleValue) {
		addValue(styleKey , styleValue , this.styles);
	}
	
	/**
	 * 添加绑定事件
	 * 
	 * @param event
	 *            事件名称
	 * @param triggerFunction
	 *            出发的方法
	 */
	public void addEvent(String event , String triggerFunction) {
		this.events.put(event , triggerFunction);
	}
	
	/**
	 * 深度复制该当前对象
	 */
	@Override
	public Permission clone() throws CloneNotSupportedException {
		Permission p = (Permission)super.clone();// 浅复制基础类型属性
		// 深度复制集合类型属性
		p.classes = new ArrayList<String>();
		p.attrs = new ArrayList<Map<String , Object>>();
		p.styles = new ArrayList<Map<String , Object>>();
		p.events = new HashMap<String , String>();
		if(this.classes != null && !this.classes.isEmpty()) {
			for(String classe : classes) {
				p.classes.add(classe);
			}
		}
		if(this.attrs != null && !this.attrs.isEmpty()) {
			for(Map<String , Object> map : this.attrs) {
				Set<String> keySet = map.keySet();
				for(String key : keySet) {
					Object value = map.get(key);
					Map<String , Object> m = new HashMap<String , Object>();
					m.put(key , value);
					p.attrs.add(m);
				}
			}
		}
		if(this.styles != null && !this.styles.isEmpty()) {
			for(Map<String , Object> map : this.styles) {
				Set<String> keySet = map.keySet();
				for(String key : keySet) {
					Object value = map.get(key);
					Map<String , Object> m = new HashMap<String , Object>();
					m.put(key , value);
					p.styles.add(m);
				}
			}
		}
		if(this.events != null && !this.events.isEmpty()) {
			Set<String> keySet = this.events.keySet();
			for(String key : keySet) {
				String value = this.events.get(key);
				Map<String , Object> m = new HashMap<String , Object>();
				m.put(key , value);
				p.events.put(key , value);
			}
		}
		return p;
	}
	
	/**
	 * 设置成函数
	 * 
	 * @param key
	 *            参数key
	 * @param value
	 *            参数值
	 */
	public void setParams(String key , Object value) {
		if(value != null) {
			if(this.attrs != null && !this.attrs.isEmpty()) {
				for(Map<String , Object> map : this.attrs) {
					Set<String> keySet = map.keySet();
					for(String k : keySet) {
						Object v = map.get(k);
						if(value != null && v.toString().contains(":" + key)) {
							v = v.toString().replaceAll(":" + key , value.toString());
							map.put(k , v);
						}
					}
				}
			}
			if(this.styles != null && !this.styles.isEmpty()) {
				for(Map<String , Object> map : this.styles) {
					Set<String> keySet = map.keySet();
					for(String k : keySet) {
						Object v = map.get(k);
						if(value != null && v.toString().contains(":" + key)) {
							v = v.toString().replaceAll(":" + key , value.toString());
							map.put(k , v);
						}
					}
				}
			}
			if(this.classes != null && !this.classes.isEmpty()) {
				List<String> oldClasses = new ArrayList<String>();
				List<String> newClasses = new ArrayList<String>();
				for(String cls : this.classes) {
					if(cls != null && cls.contains(":" + key)) {
						oldClasses.add(cls);
						cls = cls.replaceAll(":" + key , value.toString());
						newClasses.add(cls);
					}
				}
				this.classes.removeAll(oldClasses);
				this.classes.addAll(newClasses);
			}
			if(this.events != null && !this.events.isEmpty()) {
				Set<String> keySet = this.events.keySet();
				for(String k : keySet) {
					String v = this.events.get(k);
					if(value != null && v.contains(":" + key)) {
						v = v.replaceAll(":" + key , value.toString());
						this.events.put(k , v);
					}
				}
			}
			if(this.innerHtml != null && this.innerHtml.contains(":" + key)) {
				this.innerHtml = this.innerHtml.replaceAll(":" + key , value.toString());
			}
			if(this.id != null && this.id.contains(":" + key)) {
				this.id = this.id.replaceAll(":" + key , value.toString());
			}
		}
	}
	
	/**
	 * 获取值
	 * 
	 * @param separator
	 *            分隔符
	 * @param list
	 *            容器
	 * @return
	 */
	private String getValue(String separator , List<Map<String , Object>> list , boolean isWrap) {
		if(list == null || list.isEmpty()) {
			return "";
		}
		StringBuffer styleSb = new StringBuffer(" ");
		for(Map<String , Object> style : list) {
			Set<String> keys = style.keySet();
			for(String key : keys) {
				Object value = style.get(key);
				if(!isWrap) {
					styleSb.append(key + separator + value + ";");
				} else {
					styleSb.append(key + separator + "\"" + value + "\" ");
				}
			}
		}
		return styleSb.toString();
	}
	
	/**
	 * 添加至
	 * 
	 * @param key
	 *            key值
	 * @param value
	 *            value值
	 * @param list
	 *            容器
	 */
	private void addValue(String key , String value , List<Map<String , Object>> list) {
		for(Map<String , Object> map : list) {
			if(map.containsKey(key)) {
				map.put(key , value);
				return;
			}
		}
		Map<String , Object> style = new HashMap<String , Object>();
		style.put(key , value);
		list.add(style);
	}
	
	@Override
	public String toString() {
		StringBuffer outSb = new StringBuffer("");
		outSb.append("<" + tagName + " ");
		outSb.append("permissionCode=\"" + code + "\" ");
		String idStr = this.getId();
		if(StringUtils.isNotBlank(idStr)) {
			outSb.append("id=\"" + idStr + "\" ");
		}
		String classesStr = this.getClasses();
		if(StringUtils.isNotBlank(classesStr)) {
			outSb.append("class=\"" + classesStr + "\" ");
		}
		String stylesStr = this.getStyles();
		if(StringUtils.isNotBlank(stylesStr)) {
			outSb.append("style=\"" + stylesStr + "\" ");
		}
		outSb.append(this.getAttrs() + " ");
		outSb.append(this.getEvents() + " ");
		outSb.append(">");
		outSb.append(innerHtml);
		outSb.append("</" + tagName + ">");
		return outSb.toString();
	}
	
}
